package org.javaforever.gatescore.reports.verb;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.javaforever.gatescore.core.FrontCodeBlock;
import org.javaforever.gatescore.core.FrontDomain;
import org.javaforever.gatescore.core.FrontDropdown;
import org.javaforever.gatescore.core.FrontMethod;
import org.javaforever.gatescore.core.FrontVerb;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;
import org.javaforever.gatescore.utils.StringUtil;
import org.javaforever.gatescore.verb.ListActive;

public class CompareSetup extends FrontVerb{
	private static final long serialVersionUID = 1L;
	protected ListActiveCompareOption listActiveOption;
	protected ListActiveCompareSumOption listActiveSumOption;
	protected FrontDomain planDomain;
	protected FrontDomain actionDomain;

	public CompareSetup(FrontDomain planDomain,FrontDomain actionDomain,ListActiveCompareOption listActiveCompareOption,ListActiveCompareSumOption listActiveSumOption) {
		super();
		this.setVerbName("Setup");
		this.setLabel("图表初始化");
		this.domain = planDomain;
		this.planDomain = planDomain;
		this.actionDomain = actionDomain;
		this.listActiveOption = listActiveCompareOption;
		this.listActiveSumOption = listActiveSumOption;
	}

	@Override
	public FrontMethod generateControllerMethod() throws Exception {
		FrontMethod method = new FrontMethod();
		method.setStandardName("setup");
		List<Writeable> sList = new ArrayList<Writeable>();
		
		sList.add(new Statement(1000L,0,"setup() {"));
		long serial = 2000L;
		Set<FrontDomain> targets = new TreeSet<>();
		if (this.listActiveOption.getPlanyName() instanceof FrontDropdown) {
			FrontDomain target = ((FrontDropdown)this.listActiveOption.getPlanyName()).getTarget();
			targets.add(target);
		}
		if (this.listActiveOption.getActionyName() instanceof FrontDropdown) {
			FrontDomain target = ((FrontDropdown)this.listActiveOption.getActionyName()).getTarget();
			targets.add(target);
		}
		for (FrontDomain target:targets) {
			ListActive la = new ListActive(target);
			sList.add(new Statement(serial,1,"this."+StringUtil.lowerFirst(la.getVerbName())+"()"));
			serial += 2000L;
		}
		ListActive lar = new ListActive(this.listActiveOption.getPlanDomain());
		sList.add(new Statement(serial+3000L,1,"this."+StringUtil.lowerFirst(lar.getVerbName())+"()"));
		ListActive lara = new ListActive(this.listActiveOption.getActionDomain());
		sList.add(new Statement(serial+3000L,1,"this."+StringUtil.lowerFirst(lara.getVerbName())+"()"));
		sList.add(new Statement(serial+4000L,1,"setTimeout(() => {"));
		sList.add(new Statement(serial+5000L,2,"var chartstype = this.chartType"));
		sList.add(new Statement(serial+6000L,2,"var reporttype = this.dataType"));
		sList.add(new Statement(serial+7000L,2,"let myChart = this.$echarts.init(document.getElementById('main'))"));
		sList.add(new Statement(serial+8000L,2,"if (reporttype === 'sum') {"));
		sList.add(new Statement(serial+9000L,3,"this."+StringUtil.lowerFirst(this.listActiveSumOption.getVerbName())+"(chartstype)"));
		sList.add(new Statement(serial+10000L,3,"myChart.setOption(this.option)"));
		sList.add(new Statement(serial+11000L,2,"} else {"));
		sList.add(new Statement(serial+12000L,3,"this."+StringUtil.lowerFirst(this.listActiveOption.getVerbName())+"(chartstype)"));
		sList.add(new Statement(serial+13000L,3,"myChart.setOption(this.option)"));
		sList.add(new Statement(serial+14000L,2,"}"));
		sList.add(new Statement(serial+15000L,1,"}, 1500)"));
		sList.add(new Statement(serial+16000L,0,"},"));

		method.setMethodStatementList(WriteableUtil.merge(sList));
		return method;
	}

	@Override
	public FrontMethod generateApiMethod() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public FrontCodeBlock generateRouteBlockBlock() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public FrontDomain getDomain() {
		return domain;
	}

	public void setDomain(FrontDomain domain) {
		this.domain = domain;
	}

	public ListActiveCompareOption getListActiveOption() {
		return listActiveOption;
	}

	public void setListActiveOption(ListActiveCompareOption listActiveOption) {
		this.listActiveOption = listActiveOption;
	}

	public ListActiveCompareSumOption getListActiveSumOption() {
		return listActiveSumOption;
	}

	public void setListActiveSumOption(ListActiveCompareSumOption listActiveSumOption) {
		this.listActiveSumOption = listActiveSumOption;
	}

	public FrontDomain getPlanDomain() {
		return planDomain;
	}

	public void setPlanDomain(FrontDomain planDomain) {
		this.planDomain = planDomain;
	}

	public FrontDomain getActionDomain() {
		return actionDomain;
	}

	public void setActionDomain(FrontDomain actionDomain) {
		this.actionDomain = actionDomain;
	}
	@Override
	public FrontMethod generateControllerMethod(String indexSuffix) throws Exception {
		return generateControllerMethod();
	}
}
